// 生产计划

<template>
  <view>
    <mes-search
      placeholder="计划编号"
      v-model="postForm.purchase_code"
      @search="resetPaging(), getData()"
    ></mes-search>
    <mes-card-index :form="cardIndexForm"> </mes-card-index>
    <mes-tip v-if="noData"></mes-tip>
    <mes-button :form="addForm" type="1"></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 按钮
      addForm: {
        addEvent: () => {
          uni.navigateTo({ url: '/pages/home/prod_plan/add' })
        }
      },
      cardIndexForm: {
        items: [],
        more: (row) => {
          console.log('调用more')
          let examine = row.examine_status == '1' ? 'examine_f' : 'examine'
          this.cardIndexForm.actionSheet[0] = {
            name: examine == 'examine' ? '审核' : '反审核',
            event: () => {
              let postForm = {
                id: row.id,
                type: examine
              }
              this.$api.prod.plan
                .examine(postForm)
                .then((res) => {
                  if (res.code == 200) {
                    uni.showToast({
                      title: res.msg,
                      icon: 'none'
                    })
                    this.resetPaging()
                    this.getData()
                  }
                })
                .catch()
            }
          }
        },
        actionSheet: [
          {
            name: '审核/反审核',
            event: (row) => {}
          },
          {
            name: '详情',
            event: (row) => {
              uni.navigateTo({
                url: `/pages/home/prod_plan/detail?id=${row.id}`
              })
              console.log('点击详情', row)
            }
          },
          {
            name: '编辑',
            event: (row) => {
              if (row.examine_status == '1') {
                return this.$tools.toast('不能编辑已审核单据')
              }
              uni.navigateTo({
                url: `/pages/home/prod_plan/add?id=${row.id}`
              })
            }
          },
          {
            name: '删除',
            showModal: true,
            event: (row) => {
              if (row.examine_status == '1') {
                return this.$tools.toast('不能删除已审核单据')
              }
              this.del(row)
            }
          }
        ]
      },
      noData: false,
      hasMore: true,
      postForm: {
        purchase_code: '',
        page: 1
      }
    }
  },
  onShow() {
    this.resetPaging()
    this.getData()
  },
  onReachBottom() {
    if (!this.hasMore) return
    this.postForm.page++
    this.getData()
  },
  methods: {
    // 重置分页
    resetPaging() {
      this.hasMore = true
      this.postForm.page = 1
      this.cardIndexForm.items = []
    },
    // 获取数据
    async getData() {
      const res = await this.$api.prod.plan.index(this.postForm)
      if (res.code == 200) {
        this.hasMore = res.data.list.length ? true : false
        const list = []
        res.data.list.forEach((item) => {
          list.push({
            row: item,
            head: {
              tags: [
                {
                  name: item.examine_status == '1' ? '已审核' : '未审核',
                  status: item.examine_status == '1' ? 1 : 2
                }
              ],
              title: item.purchase_code,
              price: item.all_money
            },
            body: [
              {
                name: '单据日期',
                value: item.purchase_time
              },
              {
                name: '交货时间',
                value: item.delivery_time
              },
              {
                name: '车间',
                value: item.workshop_id
              },
              {
                name: '生产商品',
                value: item.goods_info
              },
              {
                name: '数量',
                value: item.number
              }
            ]
          })
        })

        this.cardIndexForm.items = [...this.cardIndexForm.items, ...list]
        this.noData = !this.cardIndexForm.items.length
      }
    },
    // 删除
    async del(row) {
      const res = await this.$api.prod.plan.del({
        id: row.id
      })
      if (res.code == 200) {
        uni.showToast({
          title: res.msg,
          icon: 'none'
        })
        this.resetPaging()
        this.getData()
      }
    }
  }
}
</script>

<style>
page {
  padding: 92rpx 0 400rpx;
}
</style>
